package marathon;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class MarathonSource {
	private volatile Date dateFirst = null;

	private class PageRetriever implements Runnable {
		private volatile String elements = new String();
		private volatile String url;

		public PageRetriever(String _url) {
			url = _url;
		}

		@Override
		public void run() {
			// System.out.println("Start of thread");
			Document doc;
			elements = new String();
			try {
				InputStream input = new URL(url).openStream();
				doc = Jsoup.parse(input, "UTF-8", url);

				// doc = Jsoup.connect(url).timeout(0).get();
				Elements el = doc.select("div.view > div.exercise");

				for (Element element : el) {
					elements += "<div class=\"exercise\" id=\"" + element.attr("id") + "\">";
					elements += element.html();
					elements += "</div>";
				}

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		public String getElements() {
			return elements;
		}
	}

	public Document merge(Vector<String> urls) {
		Document doc = null;
		Vector<PageRetriever> pages = new Vector<PageRetriever>();
		Vector<Thread> threads = new Vector<Thread>();
		
		for (String url : urls) {
			PageRetriever foo = new PageRetriever(url);
			Thread t = new Thread(foo);
			threads.add(t);
			pages.add(foo);
			t.start();
		}

		for (Thread thread : threads) {
			try {
				thread.join();
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
		}

		Writer writer = null;
		try {
			writer = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream("lidingo.xml"), "utf-8"));
			writer.write((new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"))
					.format(new Date()));
			String s = new String();
			for (PageRetriever p : pages) {
				s += p.getElements();

			}

			doc = Jsoup.parseBodyFragment(s);
			filter(doc);
			
			writer.write(doc.html());
			writer.write("</root>");

		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} finally {
			try {
				writer.close();
			} catch (Exception ex) {
			}
		}

		return doc;
	}

	private void filter(Document doc) {
		doc.select("div.day-calendar").remove();
		doc.select("div.exercise-head > span.h2").remove();
	}

}
